package mathgame.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

public class PracticePanel extends GamePanel implements Observer {

    private JCheckBox additionCBox, subtractionCBox, divisionCBox,
            multiplicationCBox, percentageCBox,
            equationCBox;
    private JLabel jProblemLbl, jFeedBackLbl;
    private JButton jBackBtn, jStartGameBtn, jAnswerBtn, jNewProblemBtn;
    private ImageIcon icon;
    private JPanel comboBoxPanel;
    private JTextField answerField;
    private JTextArea problemTextArea;

    public PracticePanel(){

        controller.getGameState().addObserver(this);

        this.setLayout(null);
        icon = createImageIcon("../source/StartPanel2.png", "background");
        comboBoxPanel = new JPanel();
        comboBoxPanel.setLayout(null);
        additionCBox = new JCheckBox("Addition");
        additionCBox.setFont(new Font("monospaced", Font.BOLD, 20));
        subtractionCBox = new JCheckBox();
        subtractionCBox.setFont(new Font("monospaced", Font.BOLD, 20));
        subtractionCBox.setText("Subtraction");
        divisionCBox = new JCheckBox();
        divisionCBox.setFont(new Font("monospaced", Font.BOLD, 20));
        divisionCBox.setText("Division");
        multiplicationCBox = new JCheckBox();
        multiplicationCBox.setFont(new Font("monospaced", Font.BOLD, 20));
        multiplicationCBox.setText("Multiplication");
        percentageCBox = new JCheckBox();
        percentageCBox.setFont(new Font("monospaced", Font.BOLD, 20));
        percentageCBox.setText("Percentages");
        equationCBox = new JCheckBox();
        equationCBox.setFont(new Font("monospaced", Font.BOLD, 20));
        equationCBox.setText("Equations");
        jAnswerBtn = new JButton();
        jAnswerBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jAnswerBtn.setForeground(Color.GREEN);
        jAnswerBtn.setText("Answer");
        jBackBtn = new JButton();
        jBackBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jBackBtn.setText("BACK");
        jStartGameBtn = new JButton();
        jStartGameBtn.setFont(new Font("monospaced", Font.BOLD, 25));
        jStartGameBtn.setForeground(Color.GREEN);
        jStartGameBtn.setText("START");
        JLabel jChapter = new JLabel("Choose Chapter(s)");
        jChapter.setFont(new Font("monospaced", Font.BOLD, 25));
        jChapter.setForeground(new Color(99,184,255));
        jProblemLbl = new JLabel();
        jProblemLbl.setFont(new Font("monospaced", Font.BOLD, 25));
        answerField = new JTextField();
        answerField.setFont(new Font("monospaced", Font.BOLD, 20));
        problemTextArea = new JTextArea();
        problemTextArea.setFont(new Font("monospaced", Font.PLAIN, 15));
        jFeedBackLbl = new JLabel();
        jFeedBackLbl.setFont(new Font("monospaced", Font.BOLD, 20));
        jNewProblemBtn = new JButton();
        jNewProblemBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jNewProblemBtn.setText("New problem");
        jNewProblemBtn.setForeground(new Color(99,184,255));

        jBackBtn.addActionListener(actionListener);
        jStartGameBtn.addActionListener(actionListener);
        additionCBox.addActionListener(actionListener);
        subtractionCBox.addActionListener(actionListener);
        divisionCBox.addActionListener(actionListener);
        multiplicationCBox.addActionListener(actionListener);
        equationCBox.addActionListener(actionListener);
        percentageCBox.addActionListener(actionListener);
        jAnswerBtn.addActionListener(actionListener);
        jNewProblemBtn.addActionListener(actionListener);
        answerField.addActionListener(actionListener);


        comboBoxPanel.add(jStartGameBtn);
        comboBoxPanel.add(jChapter);
        comboBoxPanel.add(additionCBox);
        comboBoxPanel.add(subtractionCBox);
        comboBoxPanel.add(multiplicationCBox);
        comboBoxPanel.add(divisionCBox);
        comboBoxPanel.add(percentageCBox);
        comboBoxPanel.add(equationCBox);
        this.add(jBackBtn);
        this.add(jExitBtn);
        this.add(jHelpBtn);
        this.add(comboBoxPanel);
        this.add(jProblemLbl);
        this.add(answerField);
        this.add(jAnswerBtn);
        this.add(problemTextArea);
        this.add(jFeedBackLbl);
        this.add(jNewProblemBtn);

        Insets insets = this.getInsets();
        Insets insets2 = comboBoxPanel.getInsets();

        int WIDTH_LARGE = 180;
        int HEIGHT_LARGE = 80;
        int WIDTH_SMALL = 150;
        int HEIGHT_SMALL = 40;

        jExitBtn.setBounds(800 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        comboBoxPanel.setBounds(400 + insets.left, insets.top, 600, 500);
        jHelpBtn.setBounds(620 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        jStartGameBtn.setBounds(190 + insets2.left, 380 + insets.top, 250, HEIGHT_LARGE);
        jChapter.setBounds(190 + insets2.left, 100 + insets.top, 300, HEIGHT_SMALL);
        jBackBtn.setBounds(440 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        additionCBox.setBounds(120 + insets2.left, 180 + insets.top, 210, HEIGHT_SMALL);
        subtractionCBox.setBounds(120 + insets2.left, 240 + insets.top, 210, HEIGHT_SMALL);
        divisionCBox.setBounds(120 + insets2.left, 300 + insets.top, 210, HEIGHT_SMALL);
        multiplicationCBox.setBounds(350 + insets2.left, 180 + insets.top, 210, HEIGHT_SMALL);
        percentageCBox.setBounds(350 + insets2.left, 240 + insets.top, 210, HEIGHT_SMALL);
        equationCBox.setBounds(350 + insets2.left, 300 + insets.top, 210, HEIGHT_SMALL);
        jProblemLbl.setBounds(400 + insets.left, 400 + insets.top, 200, HEIGHT_SMALL);
        answerField.setBounds(630 + insets.left, 400 + insets.top, 120, HEIGHT_SMALL);
        jAnswerBtn.setBounds(780 + insets.left, 380 + insets.top, WIDTH_LARGE, HEIGHT_LARGE);
        jFeedBackLbl.setBounds(450 + insets.left, 280 + insets.top, 450, HEIGHT_SMALL);
        problemTextArea.setBounds(30 + insets.left, 260 + insets.top, 620, 200);
        jNewProblemBtn.setBounds(780 + insets.left, 320 + insets.top, WIDTH_LARGE, HEIGHT_SMALL);
        problemTextArea.setOpaque(false);

        jNewProblemBtn.setVisible(false);
        jFeedBackLbl.setVisible(false);
        problemTextArea.setEditable(false);
        problemTextArea.setVisible(false);
        jAnswerBtn.setVisible(false);
        answerField.setVisible(false);
        jProblemLbl.setVisible(false);
        this.setOpaque(false);
        comboBoxPanel.setOpaque(false);
    }

    protected void paintComponent(Graphics g){
        g.drawImage(icon.getImage(), 0, 0, null);
        super.paintComponent(g);
    }

    ActionListener actionListener = new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(e.getSource() == additionCBox){
                controller.activateAdditionChapter();
            }
            if(e.getSource() == subtractionCBox){
                controller.activateSubtractionChapter();
            }
            if(e.getSource() == multiplicationCBox){
                controller.activateMultiplicationChapter();
            }
            if(e.getSource() == divisionCBox){
                controller.activateDivisionChapter();
            }
            if(e.getSource() == percentageCBox){
                controller.activatePercentChapter();
            }
            if(e.getSource() == equationCBox){
                controller.activateEquationChapter();
            }
            else if(e.getSource() == jBackBtn){
                controller.changeCard(1);
                controller.clearPracticeChapters();
                comboBoxPanel.setVisible(true);
                jAnswerBtn.setVisible(false);
                answerField.setVisible(false);
                jProblemLbl.setVisible(false);
                problemTextArea.setVisible(false);
                jNewProblemBtn.setVisible(false);
                jFeedBackLbl.setVisible(false);
                jFeedBackLbl.setText("");
                controller.clearProblemLbl();
                clearComboBoxes();
            }

            else if(e.getSource() == jStartGameBtn){
                if(controller.empty()){
                    JOptionPane.showMessageDialog(null, "You have to pick at least one chapter");
                }
                else{
                    controller.startPracticeGame();
                    jProblemLbl.setVisible(true);
                    comboBoxPanel.setVisible(false);
                    answerField.setVisible(true);
                    jAnswerBtn.setVisible(true);
                    jFeedBackLbl.setVisible(true);
                    jNewProblemBtn.setVisible(true);

                }
            }
            else if(e.getSource() == jAnswerBtn){
                controller.setPracticeAnswer(answerField.getText());
                answerField.setText("");
                jProblemLbl.setText("");
            }
            else if(e.getSource() == answerField){
                controller.setPracticeAnswer(answerField.getText());
                answerField.setText("");
            }
            else if(e.getSource() == problemTextArea){
                String answer = problemTextArea.getText();
                controller.setPracticeAnswer(answer);
                problemTextArea.setText("");
            }
            else if(e.getSource() == jNewProblemBtn){
                controller.newProblem();
                jFeedBackLbl.setText("");
            }
        }
    };

    public void update(Observable o, Object arg) {
        if(arg instanceof String[]){
            String[] s = (String[]) arg;
            if(s[0].length()<10){
                jProblemLbl.setText(s[0]);
                jProblemLbl.setVisible(true);
                problemTextArea.setVisible(false);
            }
            else{
                problemTextArea.setVisible(true);
                jProblemLbl.setVisible(false);
                problemTextArea.setText(s[0]);
            }
            jFeedBackLbl.setText(s[2]);
            repaint();
        }
    }

    public void clearComboBoxes(){
        additionCBox.setSelected(false);
        subtractionCBox.setSelected(false);
        divisionCBox.setSelected(false);
        multiplicationCBox.setSelected(false);
        equationCBox.setSelected(false);
        percentageCBox.setSelected(false);
    }
}